<?php

/**
 * 设备接口
 */
class Action
{

    private $per_page = 50;

    private $token = '';
    private $user_id = 0;
    private $user_info = array();
    private $api_stat_id = 0;

    public function __construct() {
        $this->token = an_request('token');

        if ($this->token) {
            $user_info = user_helper::get_user_info_by_token($this->token);
            if ($user_info && $user_info['status'] == 1) {
                $this->user_id = $user_info['id'];
                $this->user_info = $user_info;
            }
        }
    }

    /**
     * 店铺已绑定设备列表
     * @author xiaolin
     * @date   2016-10-25
     * @return json
     */
    public function store_device_list() {
        $store_id = an_request('store_id');

        //加密参数
        $sign_param = array(
            'store_id' => $store_id
        );

        //参数验证
        $this->id_validation($store_id, '请选择一个店铺');

        // 验证接口
        $this->api_stat_id = api_helper::check_sign($sign_param, 1);

        $filter['store_id'] = $store_id;
        $filter['status'] = 2;
        $count = _model('store_device_relation')->getTotal($filter);
        if (!$count) {
            api_helper::return_data(1, '暂无数据');
        }

        $device_list = _model('store_device_relation')->field('id', 'type', 'dev_id', 'add_time')->getList($filter, 'ORDER BY `add_time` DESC ');

        if ($device_list) {
            foreach ($device_list as $k => $v) {
                $device_list[$k]['type_name'] = device_config::$TYPES[$v['type']]['name'];
                $device_list[$k]['device_code'] = device_helper::get_device_code_by_id($v['dev_id']);
            }
        }

        api_helper::return_data(0, 'success', array(
            'device_list' => $device_list,
            'device_count' => $count,
        ), $this->api_stat_id);
    }

    /**
     * 验证id是否为空
     * @author xiaolin
     * @date   2016-08-31
     * @param int $id 要验证的id
     * @param string $info 提示消息
     * @return json
     */
    private function id_validation($id, $info = 'ID不能为空') {
        if (!$id) {
            api_helper::return_data(1, $info);
        }
    }
}